package com.example.message_queue.mqserver.datacenter;

import com.example.message_queue.common.MqException;
import com.example.message_queue.mqserver.core.Binding;
import com.example.message_queue.mqserver.core.Exchange;
import com.example.message_queue.mqserver.core.MSGQueue;
import com.example.message_queue.mqserver.core.Message;

import java.beans.BeanInfo;
import java.io.IOException;
import java.util.*;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-28
 * Time:16:52
 * Text：操作硬盘，需要使用这个类
 */
public class DiskDataCenter {
    //数据库的数据
    private DataBaseManger dataBaseManger = new DataBaseManger();
    //数据文件的数据
    private MessageFileManager messageFileManager = new MessageFileManager();

    public void init(){
        dataBaseManger.init();
    }

    public void insertExchange(Exchange exchange){
        dataBaseManger.insertExchange(exchange);
    }
    public void deleteExchange(String exchange){
        dataBaseManger.deleteExchange(exchange);
    }
    public List<Exchange> selectExchange(){
        return dataBaseManger.selectAllExchanges();
    }

    public void insertQueue(MSGQueue queue) throws IOException {
        dataBaseManger.insertQueue(queue);
        messageFileManager.createQueue(queue.getName());
    }
    public  void deleteQueue(MSGQueue queue) throws IOException {
        dataBaseManger.deleteQueue(queue.getName());
        messageFileManager.deleteQueueFiles(queue.getName());
    }
    public List<MSGQueue> selectQueue(){
        return dataBaseManger.selectAllqueue();
    }
    public void insertBinding(Binding bind){
        dataBaseManger.insertBinding(bind);
    }
    public void deleteBinding(Binding bind){
        dataBaseManger.deleteBinding(bind);
    }
    public List<Binding> selectBinding(){
        return dataBaseManger.selectAllBinding();
    }

    public void sendMessage(MSGQueue queue, Message message) throws IOException, MqException {
        messageFileManager.sendMessage(queue,message);
    }
    public void deleteMessage(MSGQueue queue, Message message) throws IOException, ClassNotFoundException, MqException {
        messageFileManager.deleteMessage(queue,message);
        if(messageFileManager.checkGC(queue.getName())){
            messageFileManager.gc(queue);
        }
    }
    public LinkedList<Message> loadAllMessage(String queueName) throws IOException, MqException, ClassNotFoundException {
       return messageFileManager.loadAllMessageFromQueue(queueName);
    }
}
